from django.db import models
from django.contrib.auth.models import AbstractBaseUser,BaseUserManager
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.models import AbstractUser

# Create your models here.
class Goods(models.Model):
    title=models.CharField(max_length=255,verbose_name="商品标题")
    score=models.FloatField(null=True,blank=True,verbose_name="评分")
    date=models.DateTimeField(null=True,blank=True,verbose_name="商品链接")
    suppliers=models.CharField(max_length=255,null=True,blank=True,verbose_name="供应商")
    region = models.CharField(max_length=255, null=True, blank=True, verbose_name="类型")
    summery=models.TextField(null=True,blank=True,verbose_name="简介")

    class Meta:
        verbose_name = "商品"
        verbose_name_plural = "商品集"

    def __str__(self):
        return self.title

class UserManager(BaseUserManager):
    def _create_user(self,username,password,email,**kwargs):
        if not username:
            raise ValueError("请输入用户名！！")
        if not password:
            raise ValueError("请输入密码！！")
        if not email:
            raise ValueError("请输入邮箱！！")
        user=self.module(username=username,email=email,**kwargs)
        user.set_password(password)
        user.save()
        return user
    def create_user(self,username,password,email,**kwargs):
        kwargs['is_superuser']=False
        return self._create_user(username,password,email,**kwargs)
    def create_superuser(self,username,password,email,**kwargs):
        kwargs['is_superuser']=True
        return self._create_user(username,password,email,**kwargs)

class UserInfo(AbstractBaseUser,PermissionsMixin):
    user_id=models.CharField(max_length=32,null=False,verbose_name="用户ID")
    username = models.CharField(max_length=255, null=False, verbose_name="用户名",unique=True)
    password = models.CharField(max_length=255, null=False, verbose_name="用户密码")
    nickname = models.CharField(max_length=255, null=False, verbose_name="用户昵称")
    sex_choice={
        (1,"男"),
        (2,"女")
    }
    sex = models.IntegerField(choices=sex_choice,null=False,verbose_name="性别",default=1)
    age = models.IntegerField(verbose_name="性别", null=False)
    email=models.EmailField(null=False,verbose_name="邮箱",unique=False)
    registration=models.DateTimeField(auto_now_add=True,verbose_name="创建时间",null=False)
    last_login = models.DateTimeField(auto_now_add=True, verbose_name="创建时间", null=False)
    USERNAME_FIELD='username'
    REQUIRED_FIELDS=['email']
    EMAIL_FIELD=('email')
    objects=UserManager()
    def __str__(self):
        return self.username